import React from 'react'
import BaseComponent from 'basecomponent'
import LinkNavbar from 'link-navbar'
import AntTable from 'anttable'
import FuncButton from "func-button";
import apis from 'apis'
import {withRouter} from 'react-router'
import './style.scss'
import {advType} from 'utils/staticType'
class Adv extends BaseComponent {
  constructor() {
    super(...arguments);
    this.state= {
      advData: [],
      currentPage: 1,
      pageSize: 10,
      advType: 0,
      isLoadingAdv: false
    }
  }
  componentDidMount() {
    let type = advType[this.props.params.splat]
    this.getAdvList(type);
  }
  componentWillReceiveProps(nextProps){
    if(!this.state.isLoadingAdv) {
      console.log('props', nextProps)
      let type = advType[nextProps.params.splat]
      this.getAdvList(type);
    }
  }

  getAdvList(type){
    this.setState({isLoadingAdv: true})
    this.advRP && this.advRP.request.abort()
    this.advRP = apis.adv.getAdvList(type);
    this.registerRequest(this.advRP.request);
    this.advRP.promise
      .then((res)=>{
        let data = res.Data.map((item) => {
          if(!item.Tag) return item
          let tags = item.Tag.split('  ')
          if(type === advType.school){
            item['school'] = tags[0] || ''
            item['titleF'] = tags[1] || ''
            item['titleS'] = tags[2] || ''
            item['job'] = tags[3]
          }
          if(type === advType.company){
            item['product'] = tags[0] || ''
            item['major'] = tags[1] || ''
          }
          if(type === advType.capital){
            item['status'] = tags[0] || ''
            item['unknown'] = tags[1] || ''
            item['unknownSub'] = tags[2] || ''
          }
          return item
        })
        res.Data&&this.setState({advData: data, advType: type});
        this.setState({isLoadingAdv: false})
      })
      .catch((err)=>{
        alert(err.msg)
        this.setState({isLoadingAdv: false})
      })
  }

  deleteCompany(text,record){
    this.deleteAdvRP && this.deleteAdvRP.request.abort()
    this.deleteAdvRP = apis.adv.deleteAdv(record.Id);
    this.registerRequest(this.deleteAdvRP.request);
    this.deleteAdvRP.promise
      .then((res)=>{
        alert('删除成功')
        let data = this.state.advData
        this.state.advData.forEach((item, index) => {
          if(item.Id === record.Id) {
            data.splice(index,1)
          }
        })
        this.setState({advData: data})
      })
      .catch((err)=>{
        alert(err.msg)
      })
  }

  watchCompany(text,record){
    let path={
      pathname:'/home/revise/adv',
      query:{
        Id:record.Id,
        type: this.state.advType
      },
      state:{
        revise: false,
        advData: record
      }
    };
    this.props.router.push(path);
  }

  topCompany(text,record){
    let advData = {}
    if(record.Order && record.Order === 1){
      advData = Object.assign({}, record, {Order: 0})
    }else{
      advData = Object.assign({}, record, {Order: 1})
    }
    this.reviseAdvRP && this.reviseAdvRP.request.abort()
    this.reviseAdvRP = apis.adv.reviseAdv(advData.Id, advData)
    this.registerRequest(this.reviseAdvRP.request)
    this.reviseAdvRP.promise
      .then((res) => {
        alert('成功')
        this.getAdvList(advType[this.props.params.splat])
      })
      .catch((err) => {
        console.log(err)
        alert(err.msg)
      })
  }

  getColumns (type) {
    let columns = [{
      title: '名称',
      dataIndex: 'Name',
      key: 'Name'
    },
      {
        title: '单位',
        dataIndex: 'SubName',
        key: 'SubName'
      },
      {
        title: '标签',
        dataIndex: 'Tag',
        key: 'Tag'
      },
      {
        title: '操作',
        key: 'action',
        render: (text, record) => (
          <span>
            <a onClick={()=>{this.topCompany(text,record)}}>
            {record.Order && record.Order === 1 ? '取消置顶' : '置顶'}
          </a>
            <span className="ant-divider"/>
          <a onClick={()=>{this.watchCompany(text,record)}}>查看</a>
            <span className="ant-divider"/>
          <a onClick={()=>{this.deleteCompany(text,record)}}>删除</a>
        </span>
        ),
      }
    ]
    return columns
  }

  render() {
    const {children} = this.props;
    const {advData, currentPage, pageSize, advType} = this.state;
    
    return (
      <div className={'sem-main-content'}>
        <div className={'class-company-page'}>
          <LinkNavbar props={this.props}/>
          {children ? children :
            <div className={'class-company-content'}>
              <FuncButton wrapperName={'class-company-FucBtn'}
                          leftText={'添加'}
                          leftTo={{pathname:'/home/add/adv',query:{Id: -1, type: advType}}}
                          showRight={false}
              />
             <AntTable columns={this.getColumns(advType)} data={advData} 
                        pagination={true} currentPage={currentPage} pageSize={pageSize}
                        onChange={(pagination)=>{
                          this.setState({
                            currentPage:pagination.current,
                            pageSize:pagination.pageSize,
                          })
                        }}/>
            </div>}
        </div>
      </div>
    );
  }
}

module.exports = withRouter(Adv);
